/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

/**
 * Log.java
 * This class holds connections, if connection is broken then it recreates it
 */

import java.sql.*;
import java.io.*;
import java.util.*;

import blueprint4j.utils.*;

public class ServerInformation {

	public static final String TABLE_NAME = "d_svr_information";
	private static boolean table_created = false;
	private static DBConnection connection = null;
	private static String server_id = null;

	private static void createTableIfNotExist(DBConnection connection)
	throws SQLException,DataException {
		if (!table_created) {
			// create table if not exists
			Statement statement = null;
			try {
				statement = connection.createStatement();
				statement.executeQuery("select * from " + TABLE_NAME);
			} catch (SQLException slqe) {
				// create the table
				statement.execute("create table " + TABLE_NAME + "( " +
				                  "server_id char(6)"+
				                  ")"
				                 );
				statement.execute("insert into " + TABLE_NAME + "(server_id) values ('"+Settings.getString("replication.default.server_id","IDNONE")+"')");
				connection.commit();
			}  finally {
				if (statement != null) {
					statement.close();
				}
			}
		}
	}

	public static String getServerId(DBConnection connection)
	throws SQLException,DataException {
		if (server_id == null) {
			createTableIfNotExist(connection);
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(Entity.limitSQL(connection,"select server_id from "+TABLE_NAME,1));
			rs.next();
			server_id = rs.getString(1);
			statement.close();
		}
		return server_id;
	}

	public static void setServerId(DBConnection connection,String p_server_id)
	throws SQLException {
		if (server_id == null || !server_id.equals(p_server_id)) {
			Statement statement = connection.createStatement();
			statement.execute("update " + TABLE_NAME + " set server_id = "+ Entity.asSQL(p_server_id));
			server_id = p_server_id;
			connection.commit();
			statement.close();
		}
	}	
}
